"""
#Color Selector by Light
Version 1.05

Copyright (c) 2008 by Alexey V. Grishin

You cannot take charge for this program



CHANGELOG
+ new feature/changed
- fixed
! removed



v1.05
+ now always on top is default
+ program starts at the desctop center


v1.04
! now 'alt' not 'ctrl' select color
+ recorgnize #color if enter it
+ now last chages at the top


v1.03
+ color box now readonly


v1.02
- forgot buffer after close


v1.01
- color box wasn't changed when color was selected using button


"""


import wx

#---------------------------------------------------------------------------

class MyFrame(wx.Frame):

    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title,
                         (400, 400), (245, 115))


        self.SetWindowStyle (self.WindowStyle | wx.STAY_ON_TOP)
        self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyEvent)

        self.SetPosition ((wx.DisplaySize()[0]/2-self.Size[0]/2, wx.DisplaySize()[1]/2-self.Size[1]/2))


        self.selectedColor = wx.Colour ()

        panel = wx.Panel(self)
        sizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)

        self.txtColor = wx.TextCtrl(panel, -1, "selected & copied #color", size=(130,-1))
        sizer.Add(self.txtColor)
        self.Bind(wx.EVT_TEXT, self.OnTypeColor, self.txtColor)

        self.btnSel = wx.Button(panel, -1, "Select color...")
        sizer.Add(self.btnSel)
        self.Bind(wx.EVT_BUTTON, self.OnSelectColorButton, self.btnSel)

        sizer.Add(wx.StaticText(panel, -1, "Alt - to select && copy #color"))

        self.colorBox = wx.TextCtrl (panel, -1, style=wx.TE_READONLY, size=(80,-1))
        sizer.Add(self.colorBox)

        self.cbOnTop = wx.CheckBox (panel, -1, "Always on top")
        self.cbOnTop.SetValue (True)
        sizer.Add(self.cbOnTop)
        self.Bind(wx.EVT_CHECKBOX, self.OnOnTopCheckbox, self.cbOnTop)


        border = wx.BoxSizer()
        border.Add(sizer, 0, wx.ALL, 5)
        panel.SetSizer(border)


    def OnTypeColor (self, evt):
        if len(self.txtColor.GetValue()) not in (4, 7):
            return
        self.Bind(wx.EVT_TEXT, None, self.txtColor)
        s = self.txtColor.GetValue()[1:]
        if len(s) == 3:
            s = s[0]*2 + s[1]*2 + s[2]*2
        self.selectedColor.Set (int(s[0:2],16),int(s[2:4],16),int(s[4:6],16))
        self.colorBox.SetBackgroundColour (self.selectedColor)
        self.colorBox.Refresh()
        self.Bind(wx.EVT_TEXT, self.OnTypeColor, self.txtColor)


    def OnOnTopCheckbox (self, evt):
        if evt.IsChecked():
            self.SetWindowStyle (self.WindowStyle | wx.STAY_ON_TOP)
        else:
            self.SetWindowStyle (self.WindowStyle & ~wx.STAY_ON_TOP)


    def OnKeyEvent(self, evt):
        if evt.GetKeyCode() == wx.WXK_ALT:
            ms = wx.GetMousePosition ()
            self.selectedColor = wx.ScreenDC().GetPixel(ms.x, ms.y)
            self.txtColor.SetValue(str(self.selectedColor.GetAsString (wx.C2S_HTML_SYNTAX)))
            clipboard = wx.TheClipboard
            clipboard.Open()
            clipboard.SetData(wx.TextDataObject(str(self.selectedColor.GetAsString (wx.C2S_HTML_SYNTAX))))
            clipboard.Close()
            clipboard.Flush()
            self.colorBox.SetBackgroundColour (self.selectedColor)
            self.colorBox.Refresh()
        else:
            evt.Skip ()


    def OnSelectColorButton(self, evt):
        dlg = wx.ColourDialog(self)
        if self.selectedColor != None:
            dlg.GetColourData().SetColour (self.selectedColor)
        dlg.GetColourData().SetChooseFull(True)

        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetColourData()
            self.selectedColor = data.GetColour()
            self.txtColor.SetValue(str(self.selectedColor.GetAsString (wx.C2S_HTML_SYNTAX)))
            clipboard = wx.TheClipboard
            clipboard.Open()
            clipboard.SetData(wx.TextDataObject(str(self.selectedColor.GetAsString (wx.C2S_HTML_SYNTAX))))
            clipboard.Close()
            self.colorBox.SetBackgroundColour (self.selectedColor)
            self.colorBox.Refresh()

        dlg.Destroy()




#---------------------------------------------------------------------------

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, "#Color Selector by Light")
        frame.Show(True)
        self.SetTopWindow(frame)

        return True


app = MyApp(False)
app.MainLoop()





